package com.the12354.orena;

import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageByProjectileEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityListener;

public class OrenaEntityListener extends EntityListener {
	private static OrenaPlugin plugin;

	public OrenaEntityListener(OrenaPlugin instance) {
		plugin = instance;
	}

	public void onEntityDeath(EntityDeathEvent event) {
		if (event.getEntity() != null) {
			plugin.onDeath(event.getEntity().getEntityId());
		}
	}

	public void onEntityDamage(EntityDamageEvent event) {
		if (event.getEntity() != null && !event.isCancelled()) {
			if ((event instanceof EntityDamageByProjectileEvent)) {
				EntityDamageByProjectileEvent subEvent = (EntityDamageByProjectileEvent) event;
				plugin.onDamage(event.getEntity(), subEvent.getDamager());
			} else if ((event instanceof EntityDamageByEntityEvent)) {
				EntityDamageByEntityEvent subEvent = (EntityDamageByEntityEvent) event;
				plugin.onDamage(event.getEntity(), subEvent.getDamager());
			} else {
				plugin.onDamage(event.getEntity().getEntityId(), 0);
			}
		}
	}

	public void onEntityExplode(EntityExplodeEvent event) {
		if (event.getEntity() != null && !event.isCancelled()) {
			plugin.onDamage(event.getEntity().getEntityId(), 0);
			plugin.onDeath(event.getEntity().getEntityId());
		}
	}
}